﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.CodeDom.Compiler;
using System.Diagnostics;
using Microsoft.CSharp;
using System.IO;

namespace Commander.Core.AI {
  public class EncodingCompiler {

    public static CompilationResults CompileFile(Encoding enc) {
      CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
      string Output = @"refact.exe";
      if (File.Exists(Output)) File.Delete(Output);
      System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
      //Make sure we generate an EXE, not a DLL
      parameters.GenerateExecutable = true;
      parameters.IncludeDebugInformation = true;
      parameters.OutputAssembly = Output;
      CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, enc.ToString());

      return new CompilationResults {
        CompilerErrors = results.Errors,
        HasSucceeded = results.Errors.Count < 1,
        OutputLocation = Output
      };
    }

    public static bool CompileProject(Encoding enc) {
      throw new NotImplementedException();
    }
  }

  public class CompilationResults {
    public bool HasSucceeded { get; set; }
    public CompilerErrorCollection CompilerErrors { get; set; }
    public string OutputLocation { get; set; }
  }
}
